package com.woniuxy.permission.controller;


import com.woniu.commons.util.TokenUtil;
import com.woniu.zsd.commons.core.model.Result;
import com.woniuxy.permission.form.AddResourceForm;
import com.woniuxy.permission.service.IPermissionService;
import com.woniuxy.permission.service.IResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理模块")
public class ResourceController {
    @Resource
    private IResourceService resourceService;
    @Resource
    private TokenUtil tokenUtil;
    @Resource
    private IPermissionService permissionService;


    /**
     * 查询permission中的所有接口并赛选出需要分配的资源接口
     *
     * @return
     */
    @GetMapping("/getAllPermissions")
    @ApiOperation(value = "获取资源接口")
    public Object getAllPermissions(@RequestHeader("token") String token) {
        Integer adminId = tokenUtil.getAdminId(token);
        if (adminId == 1) {
            // 获取去除特定接口后的权限 URL 映射
            Map<String, String> permissionUrls = PermissionController.getPermissionUrls();
            return new Result<>().setData(permissionUrls).setCode(200).setMsg("获取资源信息成功");
        } else {
            return new Result<>().setMsg("权限不足，仅超级管理员可以查看所有资源接口").setCode(403);
        }
    }

    /**
     * 添加资源接口
     */
    @PostMapping("/addResource")
    @ApiOperation(value = "添加资源接口")
    public Object addResource(@RequestBody AddResourceForm form, @RequestHeader("token") String token) throws Exception {
        Integer adminId = tokenUtil.getAdminId(token);
        if (adminId == 1) {
            resourceService.addResource(form);
            return new Result<>().setMsg("添加资源成功").setCode(200);
        } else {
            return new Result<>().setMsg("权限不足，仅超级管理员可以添加资源").setCode(403);
        }
   }

    /**
     * 查看以有的资源接口
     */
    @GetMapping("/getAllResources")
    @ApiOperation(value = "查看已存在的资源接口")
    public Object getAllResources(@RequestHeader("token") String token) throws Exception {
        Integer adminId = tokenUtil.getAdminId(token);
        if (adminId == 1) {
            return new Result<>().setData(resourceService.getAllResources()).setCode(200).setMsg("获取资源信息成功");
        } else {
            return new Result<>().setMsg("权限不足，仅超级管理员可以查看所有资源接口").setCode(403);
        }
    }
    /**
     * 删除资源接口
     */
    @DeleteMapping("/deleteResource")
    @ApiOperation(value = "删除资源接口")
    public Object deleteResource(@RequestParam("resourceId") Integer resourceId, @RequestHeader("token") String token) throws Exception {
        Integer adminId = tokenUtil.getAdminId(token);
        if (adminId == 1) {
            resourceService.deleteResource(resourceId);
            return new Result<>().setMsg("删除资源成功").setCode(200);
        } else {
            return new Result<>().setMsg("权限不足，仅超级管理员可以删除资源").setCode(403);
        }
    }

}








